# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#

use strict;
use warnings;
#use PDF::ReportWriter;

require "ReportWriter150.pm";
use avsCommon;

use DBI;
use DBD::ODBC;
use Date::Calc qw(Decode_Month Today Month_to_Text);

my ($dbhstats, $sqlstats, $sthstats);

my ($vendor, $college, $source, $month, @searches, $type);
my @dbdata = ();

paramscheck();

my $fyend = $begyear;
my $fystart = $fyend - 1;
my ($month_cnt, $year_start);

#my @colleges;
#my ($collegeid, $collegename, $aleph);
#
$dbhstats = DBI->connect('dbi:ODBC:AutomatedVendorStats' ,,, { 'PrintError' => 1, 'RaiseError' => 1 })
	or die '#error: dbhlogin connect: ' . DBI->errstr;
#	
##get all the college information we need
#$sthstats = $dbhstats->prepare('SELECT CollegeId, ShortName, Aleph FROM Colleges');
#$sthstats->execute();
#@colleges = @{$sthstats->fetchall_arrayref()};
#$sthstats->finish;

# fetch searches for fy
$sqlstats = qq/EXEC dbo.GetYTDComparison $begyear, $begmonth/;
$sthstats = $dbhstats->prepare($sqlstats)
	or die '#error: sthstats prepare: ' . DBI->errstr;

my @column_headers = ($begyear - 1 .'-'. $begmonth, $begyear.'-'.$begmonth, $begyear - 1 . ' FY YTD', $begyear.' FY YTD');

# prepare to build a csv file after pdf file
use Text::CSV;
my $csv=Text::CSV->new();

#foreach my $college (@colleges) {
#	($collegeid, $collegename, $aleph) = @{$college};
	
	$sthstats->execute();
		
	setupXML();

	@dbdata = @{$sthstats->fetchall_arrayref()};

	my $rw = PDF::ReportWriter->new();
	$rw->render_report("../temp/Rpt_YTDComparison.tmp",  \@dbdata);
	$rw->{destination}="../Reports/YTDComparison_$begmonth-$fyend.pdf";
	$rw->save();

	# open and send data to the csv file
	open CSV, ">../Reports/YTDComparison_$begmonth-$fyend.csv";
	print CSV 'Vendor,SW,Type,', $begyear - 1 .'-'. $begmonth, ',', $begyear.'-'.$begmonth, ',', $begyear - 1 . ' FY YTD', ',', $begyear.' FY YTD'."\n";

	foreach my $datarow (@dbdata) {
		if ($csv->combine(@{$datarow})) {
			print CSV $csv->string(),"\n";
		}
	}

	close CSV;
#}

$sthstats->finish if ($sthstats);
$dbhstats->disconnect if ($dbhstats);

sub setupXML {
	use XML::Twig;
	use IO::File;

	$month_cnt = 1;
	$year_start = $fystart;

	my $file = 'Rpt_YTDComparison.xml';
	my $twig= new XML::Twig(
		TwigHandlers => {
			'page/header/cell[@name="month"]' => \&heading_handler_months,
			'page/header/cell[@name="rundate"]' => \&heading_handler_rundate},
		TwigRoots => {report => 1});
	$twig->parsefile($file);

	my $fh = new IO::File("../temp/Rpt_YTDComparison.tmp", "w");
	$twig->print($fh);
	close $fh;

	# put colum headers based on run time
	sub heading_handler_months {
		my ($twig_obj, $month_cell) = @_;
		$month_cell->first_child('text')->set_text(shift @column_headers);
	}

	# put the run date in header
	sub heading_handler_rundate {
		my ($twig_obj, $rundate_cell) = @_;
		my ($tyear,$tmonth,$tday) = @begdate;
		$rundate_cell->first_child('text')->set_text(Month_to_Text($tmonth).' '.$tday.', '.$tyear);
	}

}
